package main

import (
	"aflow/config"
	"aflow/database"
	"aflow/models"
	"aflow/routes"
	"database/sql"
	"log"
	"os"
	"path/filepath"
)

func main() {
	// 加载配置
	config.LoadConfig()

	// 连接数据库
	if err := database.Connect(); err != nil {
		log.Fatalf("Failed to connect to database: %v", err)
	}

	// 自动迁移模型
	db, err := database.DB.DB()
	if err != nil {
		log.Fatalf("Failed to get database instance: %v", err)
	}
	defer func(db *sql.DB) {
		err := db.Close()
		if err != nil {

		}
	}(db)

	//数据库迁移
	err = database.DB.AutoMigrate(&models.Host{}, &models.Pipeline{})
	if err != nil {
		return
	}

	// 创建路由
	r := routes.SetupRouter()
	r.Static("/static", "./static")

	// 启动服务器
	if err := r.Run(":8080"); err != nil {
		log.Fatalf("Failed to start server: %v", err)
	}

}

func getExecutableDir() string {
	ex, err := os.Executable()
	if err != nil {
		panic(err)
	}
	return filepath.Dir(ex)
}
